package com.wlw.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.wlw.entity.Gonglue;
import com.wlw.entity.Scenery;

import edu.xjsoft.base.service.DefaultEntityManager;

@Service
@Transactional
public class GonglueService extends DefaultEntityManager<Gonglue> {
	//返回热门推荐攻略：
	public List<Gonglue> hotGonglues() {
		//limit 在HQL中是没用的
		String hql = " from Gonglue g order by g.zanCount desc ";
		/**
		 * 利用Query中的setFirstResult设置第一条、setMaxResults设置最后一条。
		 */
		Query query = getEntityDao().getSession().createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(5);
		List<Gonglue> gonglues = query.list();
		return gonglues;
	}
	//最受欢迎的攻略排行
	public List<Gonglue> hotGonglues3() {
		String hql = " from Gonglue g order by g.zanCount desc limit 0,3";
//		List<Gonglue> gonglues = getEntityDao().find(hql);
		
		Query query = getEntityDao().getSession().createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(3);
		List<Gonglue> gonglues = query.list();
		return gonglues;
	}
	//最新的攻略
	public List<Gonglue> newGonglues() {
		String hql = "from Gonglue g order by g.time desc limit 0,3";
//		List<Gonglue> gonglues = getEntityDao().find(hql);
		Query query = getEntityDao().getSession().createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(3);
		List<Gonglue> gonglues = query.list();
		return gonglues;
	}
	//搜索攻略（模糊搜索）
	public List<Gonglue> searchGonglue(String name){
		String hql = "select g from Gonglue g where g.city.name like ?";
		List<Gonglue> gonglues = getEntityDao().find(hql, "%"+name+"%");
		return gonglues;
	}
	//新增攻略：
//	public void saveGonglue(int id ,int zanCount,int browserCount,int downloadCount,String content,String time){
//		
//	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Gonglue gonglue) {
		getEntityDao().getSession().save(gonglue);
	}
}
